using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using System;

namespace Daan
{
    [RequireComponent(typeof(NavMeshAgent))]
    public class RigidbodyAgentComponent : MonoBehaviour
    {
        private NavMeshAgent agent;
        private Rigidbody rigid;
        private float disBetweenAgentAndRigid;
        private Vector3? des;
        private Action onCompleted;
        private bool isPaused;
        private bool needKill = false;

        private void Awake()
        {
            this.agent = this.GetComponent<NavMeshAgent>();
            {
                this.agent.updateRotation = false;
                this.agent.speed = 0;
            }
            this.rigid = this.GetComponentInParent<Rigidbody>();
        }

        private void Update()
        {
            if (this.agent.isOnNavMesh)
            {
                if (this.needKill)
                {
                    this.needKill = false;
                    this.agent.ResetPath();
                }
                this.agent.isStopped = this.isPaused;
            }
            this.disBetweenAgentAndRigid = Vector3.Distance(this.rigid.transform.position, this.agent.transform.position);
            if (!this.agent.isOnNavMesh)
            {
                this.agent.transform.position = this.rigid.transform.position.NewY(0.1F);
            }
            if (this.disBetweenAgentAndRigid >= 0.1F)
            {
                this.agent.Warp(this.rigid.transform.position);
                this.agent.transform.position = this.rigid.transform.position.NewY(0.1F);
                if (this.des != null) this.Move(this.des.Value, this.onCompleted);
            }
            if (this.agent.isOnNavMesh && this.agent.hasPath && this.IsMoveDone())
            {
                this.des = null;
                this.isPaused = true;
                this.needKill = true;
                this.onCompleted();
            }
        }

        public Vector3? GetNextPoint()
        {
            if (this.agent.isOnNavMesh && !this.agent.isStopped && this.agent.hasPath && this.agent.path.corners.Length > 1)
            {
                return this.agent.path.corners[1];
            }
            return null;
        }

        public bool Move(Vector3 point, Action onCompleted)
        {
            if (NavMesh.SamplePosition(point, out var hit, 1000, NavMesh.AllAreas))
            {
                this.des = hit.position;
                if (this.agent.isOnNavMesh)
                {
                    this.onCompleted = onCompleted;
                    this.isPaused = this.agent.isStopped = false;
                    this.needKill = false;
                    this.agent.SetDestination(this.des.Value);
                    return true;
                }
            }
            return false;
        }

        public void Stop()
        {
            this.onCompleted = null;
            this.des = null;
            this.isPaused = true;
            this.needKill = true;
        }

        public void Pause()
        {
            this.isPaused = true;
        }

        public void Resume()
        {
            this.isPaused = false;
        }

        public bool IsMoveDone()
        {
            if (!this.agent.isOnNavMesh) return false;
            var agentDone = this.agent.hasPath && this.agent.remainingDistance <= this.agent.stoppingDistance;
            return agentDone;
        }
    }
}
